﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic.Web;
using LotteryVote.Entity;

namespace LotteryVote.Controls
{
    public partial class ReportSearchBar : AgentControl
    {
        public DateTime FromDate
        {
            get
            {
                DateTime time;
                if (string.IsNullOrEmpty(txt_fromDate.Text) || !DateTime.TryParse(txt_fromDate.Text, out time))
                    FromDate = time = DateTime.Today;
                return time;
            }
            set
            {
                txt_fromDate.Text = value.ToString("yyyy-MM-dd");
            }
        }
        public DateTime ToDate
        {
            get
            {
                DateTime time;
                if (string.IsNullOrEmpty(txt_toDate.Text) || !DateTime.TryParse(txt_toDate.Text, out time))
                    ToDate = time = DateTime.Today;
                return time;
            }
            set
            {
                txt_toDate.Text = value.ToString("yyyy-MM-dd");
            }
        }
        public event EventHandler OnSearchClick, OnReturnClick;
        public void SearchUser(User user)
        {
            if (CurrentUser.Role < user.Role)
                btn_return.Visible = true;
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnInit(EventArgs e)
        {
            InitDates();
            base.OnInit(e);
        }
        void InitDates()
        {
            DateTime from, to;
            if (!string.IsNullOrEmpty(Request["From"]) && DateTime.TryParse(Request["From"], out from))
                FromDate = from;
            if (!string.IsNullOrEmpty(Request["To"]) && DateTime.TryParse(Request["To"], out to))
                ToDate = to;
        }

        #region Events
        protected void btn_search_Click(object sender, EventArgs e)
        {
            if (OnSearchClick != null)
                OnSearchClick(sender, e);
        }

        protected void btn_today_Click(object sender, EventArgs e)
        {
            FromDate = ToDate = DateTime.Today;
            btn_search_Click(sender, e);
        }

        protected void btn_yesterday_Click(object sender, EventArgs e)
        {
            FromDate = ToDate = DateTime.Today.AddDays(-1);
            btn_search_Click(sender, e);
        }

        protected void btn_thisWeek_Click(object sender, EventArgs e)
        {
            FromDate = DateTime.Today.AddDays(-6);
            ToDate = DateTime.Today;
            btn_search_Click(sender, e);
        }

        protected void btn_lastWeek_Click(object sender, EventArgs e)
        {
            FromDate = DateTime.Today.AddDays(-13);
            ToDate = DateTime.Today.AddDays(-6);
            btn_search_Click(sender, e);
        }

        protected void btn_thisMonth_Click(object sender, EventArgs e)
        {
            FromDate = DateTime.Now.AddDays(-1 * DateTime.Now.Day + 1);
            ToDate = DateTime.Today;
            btn_search_Click(sender, e);
        }

        protected void btn_lastMonth_Click(object sender, EventArgs e)
        {
            FromDate = DateTime.Now.AddDays(-1 * DateTime.Now.Day + 1).AddMonths(-1);
            ToDate = DateTime.Today.AddDays(-1 * DateTime.Now.Day);
            btn_search_Click(sender, e);
        }

        protected void btn_return_Click(object sender, EventArgs e)
        {
            if (OnReturnClick != null)
                OnReturnClick(sender, e);
        }
        #endregion
    }
}